function bindAll() {
    bindEditable();
    bindModal();
    bindUserSearchForm();
}

function bindEditable() {
    $.fn.editable.defaults.mode = 'inline';
    $('.myeditable').editable();
}

function bindMyForm() {
 $('form[data-async]').on('submit', function(event) {
        var $form = $(this);
        var $target = $($form.attr('data-target'));
 
        $.ajax({
            type: $form.attr('method'),
            url: $form.attr('action'),
            data: $form.serialize(),
 
            success: function(data, status) {
                $target.html(data);
                bindAll();
            },
            
            error: function() {
                $target.html("服务器错误, 操作失败.");   
            }
        });
 
        event.preventDefault();
    });
}

function bindModal() {
    $('#myModal').on('shown.bs.modal', function(event) {
        var $modal = $(this);
        var $modleBody = $modal.find("#my-modal-body");
        $modleBody.load("/accounts/add", function(tpl) {
            bindMyForm();
        });
    });
    
    $('#myModal').on('hide.bs.modal', function(event) {
        var $modal = $(this);
        var $modleBody = $modal.find("#my-modal-body");
        $modleBody.html("");
    });
}

function bindUserSearchForm() {
    $('#SearchUserForm').on('submit', function(event) {
        var $form = $(this);
        var $target = $('#main');
 
        $.ajax({
            type: $form.attr('method'),
            url: $form.attr('action'),
            data: $form.serialize(),
 
            success: function(data, status) {
                $target.html(data);
                bindAll();
            },
            
            error: function() {
                $target.html("服务器错误, 操作失败.");   
            }
        });
 
        event.preventDefault();
    });
   
   
    
    $('.SearchUserAction').on('click', function(event) {
        var $link = $(this);
        var $target = $('#main');
 
        $.ajax({
            type: "GET",
            url: $link.attr('href'),
 
            success: function(data, status) {
                $target.html(data);
                bindAll();
            },
            
            error: function() {
                $target.html("服务器错误, 操作失败.");   
            }
        });
 
        event.preventDefault();
    });
}


// Extend the callbacks to work with Bootstrap, as used in this example
// See: http://thedersen.com/projects/backbone-validation/#configuration/callbacks
_.extend(Backbone.Validation.callbacks, {
    valid: function (view, attr, selector) {
        var $el = view.$('[name=' + attr + ']'), 
            $group = $el.closest('.form-group');
        
        $group.removeClass('has-error');
        $group.find('.help-block').html('').addClass('hidden');
    },
    invalid: function (view, attr, error, selector) {
        var $el = view.$('[name=' + attr + ']'), 
            $group = $el.closest('.form-group');
        
        $group.addClass('has-error');
        $group.find('.help-block').html(error).removeClass('hidden');
    }
});

// https://github.com/hongymagic/jQuery.serializeObject
$.fn.serializeObject = function () {
    "use strict";
    var a = {}, b = function (b, c) {
        var d = a[c.name];
        "undefined" != typeof d && d !== null ? $.isArray(d) ? d.push(c.value) : a[c.name] = [d, c.value] : a[c.name] = c.value
    };
    return $.each(this.serializeArray(), b), a
};
